home *** CD-ROM | disk | FTP | other *** search
/ SGI Developer Toolbox 6.1 / SGI Developer Toolbox 6.1 - Disc 4.iso / src / exampleCode / networking / ifaddress / ifaddr.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-08-02  |  2.3 KB  |  90 lines

  1. /*
  2.  * Copyright 1992, 1993, 1994, Silicon Graphics, Inc.
  3.  * All Rights Reserved.
  4.  *
  5.  * This is UNPUBLISHED PROPRIETARY SOURCE CODE of Silicon Graphics, Inc.;
  6.  * the contents of this file may not be disclosed to third parties, copied or
  7.  * duplicated in any form, in whole or in part, without the prior written
  8.  * permission of Silicon Graphics, Inc.
  9.  *
  10.  * RESTRICTED RIGHTS LEGEND:
  11.  * Use, duplication or disclosure by the Government is subject to restrictions
  12.  * as set forth in subdivision (c)(1)(ii) of the Rights in Technical Data
  13.  * and Computer Software clause at DFARS 252.227-7013, and/or in similar or
  14.  * successor clauses in the FAR, DOD or NASA FAR Supplement. Unpublished -
  15.  * rights reserved under the Copyright Laws of the United States.
  16.  */
  17. /*
  18.  * ifaddr.c --
  19.  *
  20.  *    Prints the link-level address of an Ethernet or FDDI interface.
  21.  *
  22.  *    Usage: ifaddr [name]
  23.  *     Where "name" is the interface name used with ifconfig:
  24.  *        e.g., et0, ec0, enp0, enp1, ipg0, etc.
  25.  *     If "name" not specified, the primary interface is used.
  26.  *    Note: fddi addresses are shown in fddi bit order (which are swapped 
  27.  *    from ether bit order).
  28.  *
  29.  *    Compile:  cc ifaddr.c -lsun -o ifaddr
  30.  *
  31.  *    Must have superuser privilege to execute.
  32.  *
  33.  *    Valid for IRIX 3.3 and IRIX 4.0.
  34.  */
  35.  
  36. #include <sys/types.h>
  37. #include <sys/socket.h>
  38. #include <net/raw.h>
  39. #include <netinet/if_ether.h>
  40. #include <stdio.h>
  41. #include <string.h>
  42.  
  43. extern char *ether_ntoa();
  44.  
  45. main(int argc, char **argv)
  46. {
  47.     struct ifreq req;
  48.     struct sockaddr_raw sr;
  49.     int s;
  50.     char *iface;
  51.  
  52.     s = socket(AF_RAW, SOCK_RAW, 0);
  53.     if (s < 0) {
  54.     perror("socket");
  55.         exit(1);
  56.     }
  57.     if (argc < 2) {
  58.     int cc;
  59.  
  60.     bzero(&sr, sizeof(sr));
  61.     sr.sr_family = AF_RAW;
  62.     sr.sr_port = 0;
  63.     if (bind(s, &sr, sizeof sr) < 0) {
  64.         perror("bind");
  65.         exit(1);
  66.     }
  67.     cc = sizeof sr;
  68.     if (getsockname(s, (struct sockaddr *)&sr, &cc) < 0) {
  69.         perror("getsockname");
  70.         exit(1);
  71.     }
  72.     iface = sr.sr_ifname;
  73.     } else {
  74.     iface = argv[1];
  75.     }
  76.  
  77.     bzero(&req, sizeof(req));
  78.     (void) strncpy(req.ifr_name, iface, sizeof req.ifr_name);
  79.  
  80.     if (ioctl(s, SIOCGIFADDR, &req, sizeof(req)) < 0) {
  81.     perror("ioctl");
  82.     exit(1);
  83.     }
  84.     (void) printf("%s = %s\n", 
  85.     iface, ether_ntoa(((struct sockaddr_raw *)&req.ifr_addr)->sr_addr));
  86.  
  87.     /*NOTREACHED*/
  88.     exit(0);
  89. }
  90.